from utils import db, generate_id
from sqlalchemy import desc


class Tab(db.Model):
    __tablename__ = 'tabs'

    id = db.Column(db.String(32), unique=True, primary_key=True)
    name = db.Column(db.String(64))
    weight = db.Column(db.Integer)

    @classmethod
    def all(cls, with_tags=True):
        tabs = db.session.query(cls).all()
        for tab in tabs:
            tags = Tag.get(tab_id=tab.id)
            setattr(tab, 'tags', tags)
        return tabs

    @classmethod
    def create(cls, name, weight=None):
        if weight:
            tab = cls(id=generate_id(), name=name, weight=weight)
        else:
            tab = db.session.query(cls).order_by(desc(cls.weight)).first()
            if tab:
                number = tab.weight + 1
            else:
                number = 0
            tab = cls(id=generate_id(), name=name, weight=number)
        db.session.add(tab)
        db.session.commit()
        return tab


class Tag(db.Model):
    __tablename__ = 'tags'

    id = db.Column(db.String(32), unique=True, primary_key=True)
    name = db.Column(db.String(64))
    weight = db.Column(db.Integer)
    tab_id = db.Column(db.String(32))

    @classmethod
    def all(cls, id=None):
        if id:
            return db.session.query(cls).filter_by(id=id).all()
        else:
            return db.session.query(cls).all()

    @classmethod
    def get(cls, tag_id=None, with_tab=True):
        if tag_id:
            tag = db.session.query(cls).filter_by(id=tag_id).first()
            setattr(tag, 'tab', db.session.query(Tab))
        else:
            tags = db.session.query(cls).all()


    @classmethod
    def create(cls, name, tab_id, weight=None):
        if weight:
            tag = cls(id=generate_id(), name=name, tab_id=tab_id, weight=weight)
        else:
            tag = db.session.query(cls).order_by(desc(cls.weight)).first()
            if tag:
                number = tag.weight + 1
            else:
                number = 0
            tag = cls(id=generate_id(), name=name, tab_id=tab_id, weight=number)
        db.session.add(tag)
        db.session.commit()
        return tag


class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.String(32), unique=True, primary_key=True)
    username = db.Column(db.String(64), unique=True)
    email = db.Column(db.String(64), unique=True)
    password = db.Column(db.String(64))
    location = db.Column(db.String(64))
    signature = db.Column(db.String(128))
    
    # score >= 10000 is admin
    score = db.Column(db.Integer)


    # 给flask-login用
    def is_authenticated(self):
        return True

    # 不启用这个特性
    def is_active(self):
        return True

    # 给flask-login用
    def is_anonymous(self):
        return False

    # 返回id
    def get_id(self):
        return self.id

    @property
    def is_admin(self):
        return self.score >= 10000

class Topic(db.Model):
    __tablename__ = 'topics'
    json_columns = ['id', 'title', 'content']

    id = db.Column(db.String(32), unique=True, primary_key=True)
    title = db.Column(db.String(64))
    content = db.Column(db.String(1024))


class TopicWithTag(db.Model):
    __tablename__ = 'topic_with_tag'

    id = db.Column(db.String(32), unique=True, primary_key=True)
    topic_id = db.Column(db.String(32))
    tag_id = db.Column(db.String(32))


if __name__ == '__main__':
    from utils import create_app

    import os
    db.metadata.clear()
    db.create_all(app=create_app(os.getenv('FLASK_CONFIG') or 'default'))